package com.simen.viewpager.cards;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.simen.viewpager.R;
import com.simen.viewpager.view.CardsPagerAdapter;
import com.simen.viewpager.view.CardsViewPager;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class CardsActivity extends Activity implements View.OnClickListener {

    private CardsViewPager mViewPager;
    private SamplePageAdapter samplePageAdapter;
    private List<View> mViews = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cards);

        mViewPager = (CardsViewPager) findViewById(R.id.viewPager);
        findViewById(R.id.delete).setOnClickListener(this);

        initViews();

        mViewPager.setPageTransformer(false, new MyPageTransformer());
        samplePageAdapter = new SamplePageAdapter(mViews);
        mViewPager.setAdapter(samplePageAdapter);
        mViewPager.setOffscreenPageLimit(mViews.size());
        mViewPager.setCurrentItem(mViews.size() / 2);
    }

    private void initViews() {
        int[] colors = {0xff00ffff, 0xffff0000, 0xff0000ff};
        for (int index = 0; index < 10; index++) {
            TextView textView = new TextView(this);
            textView.setBackgroundColor(colors[index % 3]);
            textView.setText("选中:" + index);
            textView.setTextColor(Color.BLACK);

            mViews.add(textView);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.delete:
                if (!mViews.isEmpty()) {
                    mViews.remove(mViewPager.getCurrentItem());
                    samplePageAdapter.notifyDataSetChanged();
                }
                break;
        }
    }

    public class MyPageTransformer implements CardsViewPager.PageTransformer {

        private static final String TAG = "MyPageTransformer";

        private float MIN_SCALE = 0.85f;
        private float MIN_ALPHA = 0.5f;

        @Override
        public void transformPage(View view, float position) {
            int pageWidth = view.getWidth() - 100;
            int pageHeight = view.getHeight();

            Log.d(TAG, "transformPage: " + position + " " + ((TextView) view).getText().toString() + " " +
                    "" + pageWidth + " " + pageHeight);

//            if (position < -1) {
//                view.setAlpha(0);
//            } else if (position <= 1) {
//
//                // Modify the default slide transition to shrink the page as well
//                float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
//                float vertMargin = pageHeight * (1 - scaleFactor) / 2;
//                float horzMargin = pageWidth * (1 - scaleFactor) / 2;
//                if (position < 0) {
//                    view.setTranslationX(horzMargin - vertMargin / 2);
//                } else {
//                    view.setTranslationX(-horzMargin + vertMargin / 2);
//                }
//
//                // Scale the page down (between MIN_SCALE and 1)
//                view.setScaleX(MIN_SCALE);
//                view.setScaleY(MIN_SCALE);

//                // Fade the page relative to its size.
//                view.setAlpha(MIN_ALPHA + (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_ALPHA));
//            } else {
//                view.setAlpha(0);
//            }
        }
    }

    public class SamplePageAdapter extends CardsPagerAdapter {

        private List<View> mViews;

        public SamplePageAdapter(List<View> views) {
            this.mViews = views;
        }

        @Override
        public int getCount() {
            return mViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View child = mViews.get(position);
            container.addView(child);
            return child;
        }

        @Override
        public int getItemPosition(Object object) {
            Iterator<View> iterator = mViews.iterator();
            int position = 0;
            while (iterator.hasNext()) {
                if (iterator.next() == object) {
                    return position;
                }

                position++;
            }
            return PagerAdapter.POSITION_NONE;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public float getPageWidth(int position) {
//            if (getCount() > 1) {
//                if (position == 0 || getCount() - 1 == position) {
//                    return 0.95f;
//                }
//            }
            return 0.8f;
        }

    }
}
